package com.zcy.nidavellir.multilogin.login

import android.app.Activity
import android.content.Intent
import com.twitter.sdk.android.core.Callback
import com.twitter.sdk.android.core.Result
import com.twitter.sdk.android.core.TwitterException
import com.twitter.sdk.android.core.TwitterSession
import com.twitter.sdk.android.core.identity.TwitterAuthClient
import com.zcy.nidavellir.multilogin.Utils.log
import com.zcy.nidavellir.multilogin.Utils.toast
import com.zcy.nidavellir.multilogin.proxy.ILogin
import java.lang.ref.WeakReference

/**
 * @author:       zhaocy
 * @description:  Twitter login impl
 * @date:         2018/9/29
 */
class TwitterLoginImpl(private val weakReference: WeakReference<Activity>) : ILogin {
    override fun loginRequest(context: Activity, authToken: String) {
        weakReference?.get()?.toast("Twitter log in success authToken = $authToken")
        log("Twitter log in success authToken = $authToken")
    }

    override fun action() {
        val activity = weakReference.get() ?: return
        TwitterAuthClient().authorize(activity, object : Callback<TwitterSession>() {
            override fun success(result: Result<TwitterSession>) {
                loginRequest(activity, result.data.authToken.toString())
            }

            override fun failure(exception: TwitterException) {
                log("TwitterException:${exception.message}")
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        TwitterAuthClient().onActivityResult(requestCode, resultCode, data)
    }
}